<?php

namespace App\Http\Controllers\Admin;

use App\Model\SaMenu;
use App\Service\HelperClass;
use App\Service\MenuService;

class MenuController extends BaseController
{
    /**
     * 获取菜单列表.
     */
    public function getMenuList()
    {
        $where['parent_id'] = $this->requestArr['parent_id'] ?? null;

        $data   = SaMenu::getMenus($where);
        $result = getTree($data);
        $list   = getList($result);
        foreach ($list as &$val) {
            $val['status'] = 1 == $val['status'];
        }

        return successJson($list);
    }

    /**
     * 获取树形菜单.
     */
    public function getMenuTree()
    {
        $data   = SaMenu::getMenus([]);
        $result = getTree($data);

        return successJson($result);
    }

    /**
     * 获取菜单详情.
     */
    public function getMenuInfo()
    {
        $id = $this->requestArr['id'];

        $info = MenuService::getInfoById($id);

        return successJson($info);
    }

    /**
     * 添加菜单.
     */
    public function createMenu()
    {
        $loginAdmin          = HelperClass::getLoginAdmin();

        $data['alias']       = $this->requestArr['alias'] ?? '';
        $data['name']        = $this->requestArr['name'];
        $data['icon']        = $this->requestArr['icon']      ?? '';
        $data['sort']        = $this->requestArr['sort']      ?? 0;
        $data['parent_id']   = $this->requestArr['parent_id'] ?? 0;
        $data['status']      = 1 == $this->requestArr['status'] ? 1 : 0;
        $data['creator_id']  = $loginAdmin['id'];
        $data['creator']     = $loginAdmin['username'];

        $result = MenuService::addMenu($data);

        return successJson($result);
    }

    /**
     * 编辑菜单.
     */
    public function editMenu()
    {
        $id                = $this->requestArr['id'];
        $data['alias']     = $this->requestArr['alias'] ?? '';
        $data['name']      = $this->requestArr['name'];
        $data['icon']      = $this->requestArr['icon']      ?? '';
        $data['sort']      = $this->requestArr['sort']      ?? 0;
        $data['parent_id'] = $this->requestArr['parent_id'] ?? 0;
        $data['status']    = 1 == $this->requestArr['status'] ? 1 : 0;

        $loginAdmin          = HelperClass::getLoginAdmin();
        $data['updater_id']  = $loginAdmin['id'];
        $data['updater']     = $loginAdmin['username'];

        return successJson(MenuService::editMenu($id, $data));
    }

    /**
     * 快捷修改菜单.
     */
    public function quickEditMenu()
    {
        $id                  = $this->requestArr['id'];
        $loginAdmin          = HelperClass::getLoginAdmin();
        $data['updater_id']  = $loginAdmin['id'];
        $data['updater']     = $loginAdmin['username'];
        $data['status']      = 1 == $this->requestArr['status'] ? 1 : 0;

        $result = SaMenu::editMenu($id, $data);

        return successJson($result);
    }

    /**
     * 删除菜单.
     */
    public function deleteMenu()
    {
        $id = $this->requestArr['id'];

        $result = MenuService::delMenuById($id);

        return successJson($result);
    }
}
